package com.example.tse.rbyair.PKwebview;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.tse.rbyair.R;

import org.kymjs.kjframe.log.L;

import butterknife.Bind;
import butterknife.ButterKnife;


/**
 * Created by tse on 2015/11/25.
 */
@SuppressLint("ValidFragment")
public class WebViewFragment extends Fragment {
    @Bind(R.id.topTitleLeftTxt)
    TextView topTitleLeftTxt;
    @Bind(R.id.title_lay)
    RelativeLayout titleLay;
    @Bind(R.id.circle_webview)
    WebView circleWebview;
    private final String webUrl = "http://v1.meigooo.com/index.html";

    public WebViewFragment() {

    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
//        L.d("HomeFragment hidden:" + hidden);

        if (!hidden) {
            if (topTitleLeftTxt.getVisibility() == View.VISIBLE) {
                circleWebview.loadUrl(webUrl);
            }
        }
    }

    @Override
    public void setMenuVisibility(boolean menuVisible) {
        super.setMenuVisibility(menuVisible);
        if (this.getView() != null) {
            this.getView().setVisibility(menuVisible ? View.VISIBLE : View.GONE);
        }
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.main_home_fragment, null);
        ButterKnife.bind(this, view);
        topTitleLeftTxt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (circleWebview.canGoBack()) {
                    circleWebview.goBack(); // goBack()表示返回WebView的上一页面
                }
            }
        });
        topTitleLeftTxt.setVisibility(View.VISIBLE);

        circleWebview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
        WebSettings webSetting = circleWebview.getSettings();
        webSetting.setJavaScriptEnabled(true);

        webSetting.setAllowFileAccess(true);
        webSetting.setDomStorageEnabled(true);
        webSetting.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);


        String user_agent = webSetting.getUserAgentString();
        String vaule = webSetting.getUserAgentString() + " RbyApp";
        webSetting.setUserAgentString(vaule);


        circleWebview.setWebChromeClient(new WebChromeClient() {

            @Override
            public boolean onJsAlert(WebView view, String url, String message,
                                     JsResult result) {
                L.d("WebViewFragment view:" + view + " url:" + url + " message:" + message + " result" + result);
                return super.onJsAlert(view, url, message, result);

            }

        });
        circleWebview.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);


                if (url.equals(webUrl)) {
                    topTitleLeftTxt.setVisibility(View.INVISIBLE);
                } else {
                    topTitleLeftTxt.setVisibility(View.VISIBLE);

                }

//                topTitleLeftTxt

            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return super.shouldOverrideUrlLoading(view, url);

            }

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);

            }

        });

        circleWebview.loadUrl(webUrl);


        return view;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();

    }
}
